package com.lut.web;

import com.lut.pojo.Book;
import com.lut.pojo.Page;
import com.lut.service.BookService;
import com.lut.service.impl.BookServiceImpl;
import com.lut.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @user:dell
 * @date:2020年10月14 09:24
 */
public class ClientBookServlet extends BaseServlet {
    private BookService bookService=new BookServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.ParseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.ParseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page=bookService.page(pageNo,pageSize);
        page.setUrl("client/bookServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       显示的页码数
        int pageNo = WebUtils.ParseInt(req.getParameter("pageNo"), 1);
//     每页显示的内容大小
        int pageSize = WebUtils.ParseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//        最小价格
        int min = WebUtils.ParseInt(req.getParameter("min"), 0);
//        最大价格
        int max = WebUtils.ParseInt(req.getParameter("max"), Integer.MAX_VALUE);
        Page<Book> page=bookService.pageByPrice(pageNo,pageSize,min,max);

        StringBuilder sb = new StringBuilder("client/bookServlet?action=pageByPrice");
        //        如果有最小价格的参数，追加到分页条的地址参数中
        if(req.getParameter("min")!=null){
            sb.append("&min=").append(req.getParameter("min"));
        }
        //        如果有最大价格的参数，追加到分页条的地址参数中
        if(req.getParameter("max")!=null){
            sb.append("&max=").append(req.getParameter("max"));
        }
        page.setUrl(sb.toString());
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }

}
